package com.melody.experiment_7;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private ImageView imgHorn;
    private TextView txtHint;
    private TextView txtDuck_1;
    private TextView txtDuck_2;
    private TextView txtDuck_3;
    private RadioGroup radioGroup;

    IntentFilter intentFilterOne = new IntentFilter();
    IntentFilter intentFilterTwo = new IntentFilter();
    IntentFilter intentFilterThree = new IntentFilter();
    OrderlyBroadcastReceiverOne orderlyBroadcastReceiverOne = new OrderlyBroadcastReceiverOne();
    OrderlyBroadcastReceiverTwo orderlyBroadcastReceiverTwo = new OrderlyBroadcastReceiverTwo();
    OrderlyBroadcastReceiverThree orderlyBroadcastReceiverThree = new OrderlyBroadcastReceiverThree();
    private boolean isExistOperation = false;
    private boolean isSelectOperation_2 = false;
    private boolean isSelectOperation_3 = false;

    class OrderlyBroadcastReceiverOne extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            txtDuck_1.setVisibility(View.VISIBLE);
            txtDuck_1.setText("1");
            if (isSelectOperation_2 || isSelectOperation_3) {
                txtDuck_1.setText("2");
            }
            Log.d("MainActivity", "我是第1只小鸭子");
        }
    }

    class OrderlyBroadcastReceiverTwo extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            txtDuck_2.setVisibility(View.VISIBLE);
            txtDuck_2.setText("2");
            if (isSelectOperation_2) {
                txtDuck_2.setText("1");
            }
            if (isSelectOperation_3) {
                txtDuck_2.setText("1");
                abortBroadcast();
            }
            Log.d("MainActivity", "我是第2只小鸭子");
        }
    }

    class OrderlyBroadcastReceiverThree extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            txtDuck_3.setVisibility(View.VISIBLE);
            Log.d("MainActivity", "我是第3只小鸭子");
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        imgHorn.setOnClickListener(view -> {
            txtHint.setVisibility(View.VISIBLE);
            // 发送有序广播
            sendMyOrderlyBroadcast();
        });

        // 更改操作数
        radioGroup.setOnCheckedChangeListener((radioGroup, checkedId) -> {
            // 重置可见性
            resetVisibilityAndOperation();
            // 重置注册
            if (isExistOperation) {
                unRegisterReceivers();
            }
            if (checkedId == R.id.rb_1) {
                registerReceiverForOperationOne();
            } else if (checkedId == R.id.rb_2) {
                registerReceiverForOperationTwo();
            } else if (checkedId == R.id.rb_3) {
                registerReceiverForOperationThree();
            }
            isExistOperation = true;
        });
    }

    private void initView() {
        imgHorn = findViewById(R.id.img_horn);
        txtHint = findViewById(R.id.txt_hint);
        txtDuck_1 = findViewById(R.id.txt_duck_1);
        txtDuck_2 = findViewById(R.id.txt_duck_2);
        txtDuck_3 = findViewById(R.id.txt_duck_3);
        radioGroup = findViewById(R.id.radioGroup);
    }

    /**
     * 重置可见性状态
     */
    private void resetVisibilityAndOperation() {
        txtHint.setVisibility(View.INVISIBLE);
        txtDuck_1.setVisibility(View.INVISIBLE);
        txtDuck_2.setVisibility(View.INVISIBLE);
        txtDuck_3.setVisibility(View.INVISIBLE);
        isSelectOperation_2 = false;
        isSelectOperation_3 = false;
    }

    /**
     * 发送广播
     */
    private void sendMyOrderlyBroadcast() {
        Intent intent = new Intent("com.example.experiment_7.MY_ORDERLY_BROADCAST");
        intent.setPackage(getPackageName());
        sendOrderedBroadcast(intent, null);
    }

    /**
     * 注册BroadcastReceiver, 接收和处理广播(第一种操作)
     */
    private void registerReceiverForOperationOne() {
        Log.d("MainActivity", "第一种操作: 默认, 无权重设置, 无拦截");
        // 动态注册OrderlyBroadcastReceiverOne广播
        intentFilterOne.setPriority(1000);
        intentFilterOne.addAction("com.example.experiment_7.MY_ORDERLY_BROADCAST");
        registerReceiver(orderlyBroadcastReceiverOne, intentFilterOne);
        // 动态注册OrderlyBroadcastReceiverTwo广播
        intentFilterTwo.setPriority(800);
        intentFilterTwo.addAction("com.example.experiment_7.MY_ORDERLY_BROADCAST");
        registerReceiver(orderlyBroadcastReceiverTwo, intentFilterTwo);
        // 动态注册OrderlyBroadcastReceiverThree广播
        intentFilterThree.setPriority(600);
        intentFilterThree.addAction("com.example.experiment_7.MY_ORDERLY_BROADCAST");
        registerReceiver(orderlyBroadcastReceiverThree, intentFilterThree);
    }

    /**
     * 注册BroadcastReceiver, 接收和处理广播(第二种操作)
     */
    private void registerReceiverForOperationTwo() {
        Log.d("MainActivity", "第二种操作: 有权重设置(213), 无拦截");
        isSelectOperation_2 = true;
        // 动态注册OrderlyBroadcastReceiverTwo广播
        intentFilterTwo.setPriority(1000);
        intentFilterTwo.addAction("com.example.experiment_7.MY_ORDERLY_BROADCAST");
        registerReceiver(orderlyBroadcastReceiverTwo, intentFilterTwo);
        // 动态注册OrderlyBroadcastReceiverOne广播
        intentFilterOne.setPriority(1000);
        intentFilterOne.addAction("com.example.experiment_7.MY_ORDERLY_BROADCAST");
        registerReceiver(orderlyBroadcastReceiverOne, intentFilterOne);
        // 动态注册OrderlyBroadcastReceiverThree广播
        intentFilterThree.setPriority(600);
        intentFilterThree.addAction("com.example.experiment_7.MY_ORDERLY_BROADCAST");
        registerReceiver(orderlyBroadcastReceiverThree, intentFilterThree);
    }

    /**
     * 注册BroadcastReceiver, 接收和处理广播(第三种操作)
     */
    private void registerReceiverForOperationThree() {
        Log.d("MainActivity", "第三种操作: 有权重设置(213), 有拦截");
        isSelectOperation_3 = true;
        // 动态注册OrderlyBroadcastReceiverTwo广播
        intentFilterTwo.setPriority(1000);
        intentFilterTwo.addAction("com.example.experiment_7.MY_ORDERLY_BROADCAST");
        registerReceiver(orderlyBroadcastReceiverTwo, intentFilterTwo);
        // 动态注册OrderlyBroadcastReceiverOne广播
        intentFilterOne.setPriority(1000);
        intentFilterOne.addAction("com.example.experiment_7.MY_ORDERLY_BROADCAST");
        registerReceiver(orderlyBroadcastReceiverOne, intentFilterOne);
        // 动态注册OrderlyBroadcastReceiverThree广播
        intentFilterThree.setPriority(600);
        intentFilterThree.addAction("com.example.experiment_7.MY_ORDERLY_BROADCAST");
        registerReceiver(orderlyBroadcastReceiverThree, intentFilterThree);
    }

    /**
     * 取消注册
     */
    private void unRegisterReceivers() {
        unregisterReceiver(orderlyBroadcastReceiverOne);
        unregisterReceiver(orderlyBroadcastReceiverTwo);
        unregisterReceiver(orderlyBroadcastReceiverThree);
    }

}